package Operation;

import JavaBeans.*;

import java.util.Scanner;

public class RemoveOperation implements IOperation{
    @Override
    public void operate(BookList bookList) {
        System.out.println("删除图书...");
        if(bookList.getCurrentCount() == 0){
            System.out.println("当前书架为空！");
            return;
        }
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要删除的图书名：");
        String name = in.nextLine();
        for (int i = 0; i < bookList.getCurrentCount(); i++) {
            if(bookList.getBook(i).getName().equals(name)){
                //删除当前i下标上的书
                for (int j = i; j < bookList.getCurrentCount() - 1; j++) {
                    bookList.setBook(i,bookList.getBook(i + 1));
                }
                bookList.setCurrentCount(bookList.getCurrentCount() - 1);
                bookList.setBook(bookList.getCurrentCount(),null);
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("删除失败，书架中无此书！");
    }
}
